<?php
/**
 * Created by PhpStorm.
 * Login: 曹旺
 * Date: 2018-03-13
 * Time: 09:08
 */

namespace app\index\model;

use think\Db;
use think\Model;

class UserAddress extends Model
{
    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id');
    }

    /**
     * @param $id用户id
     * @return false|mixed|\PDOStatement|string|\think\Collection
     *  收货地址列表
     * @author  曹旺
     * @data  17:11
     */
    public function address_lists($id)
    {
        $result = Db::table('nbsm_user_address')->field('id,consignee,consignee_phone,address,address_info,sort')->where(['user_id' => $id, 'state' => 1])->select();
        foreach ($result as $key=>$value){
            $result[$key]['address_info']="【".$value['address']."】".$value['address_info'];
        }
        return $result;
    }

    /**
     * @param $data 【收货地址的相关信息user_id  address address_info consignee consignee_phone sort】
     * @return int|string
     * 添加收货地址
     * @author  曹旺
     * @data  17:32
     */
    public function address_adds($data)
    {
        if (isset($data['sort']) && $data['sort'] == 1) {
            $sort = Db::table('nbsm_user_address')->field('id')->where(['user_id' => $data['user_id'], 'sort' => 1])
                ->select();
            if (count($sort) > 0) {
                foreach ($sort as $k => $v) {
                    Db::table('nbsm_user_address')->where(['id' => $v['id']])->update(['sort' => 0]);
                }
            }
        }
        $result = Db::table('nbsm_user_address')->insertGetId($data);
        return $result;
    }

    /**
     * @param $id收货地址id
     * @return array|false|mixed|\PDOStatement|string|Model
     * 收货地址详情
     * @author  曹旺
     * @data  17:11
     */
    public function address_save_infos($id){
        $arr=Db::table('nbsm_user_address')->field('sort,address,address_info,id,consignee_phone,consignee')->where(['id'=>$id])->find();
        return $arr;
    }
    /**
     * @param $data
     * @return false|int|string
     * 修改收货地址
     * @author  曹旺
     * @data  17:32
     */
    public function address_saves($data)
    {
        $res=Db::table('nbsm_user_address')->where($data)->find();
        if (!empty($res)){
            return 1;
        }else{
            //取user_id如果是当前地址是默认则修改原来默认状态
            $ress=Db::table('nbsm_user_address')->field('user_id')->where(['id'=>$data['id']])->find();
            if (isset($data['sort']) && $data['sort']==1){
                Db::table('nbsm_user_address')->where(['user_id'=>$ress['user_id']])->update(['sort'=>0]);
            }
            $result = Db::table('nbsm_user_address')->update($data);
            return $result;
        }

    }

    /**
     * @param $id要删除的收货地址id
     * @return false|int|string
     * 删除收货地址
     * @author  曹旺
     * @data  14:20
     */
    public function address_dels($id){
        $res=Db::table('nbsm_user_address')->where(['id'=>$id])->update(['state'=>0]);
        return $res;
    }


    /**
     * @return false|mixed|\PDOStatement|string|\think\Collection
     * 送货的省份
     * @author  曹旺
     * @data  10:23
     */
    public function address(){
        $arr=Db::table('nbsm_pay_address')->field('province')->where('state=1')->select();
        return $arr;
    }
}